﻿using System;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using Microsoft.SharePoint.Utilities;
using System.IO;

namespace SUIE.ControlTemplates
{
    public partial class TabConfigurationEditorPartUserControl : System.Web.UI.UserControl
    {
        private TabConfigurationEditorPart parentEditorPart;

        public Boolean TabsEnabled
        {
            get {
                return chkEnabled.Checked;   
            }
            set
            {
                chkEnabled.Checked = value;
            }
        }

        public TabOrientationEnum TabOrientation
        {
            get
            {
                if(rdbVertical.Checked) {
                    return TabOrientationEnum.vertical;
                }
                else {
                    return TabOrientationEnum.horizontal;
                }                
            }
            set
            {
                if (value == TabOrientationEnum.vertical)
                {
                    rdbVertical.Checked = true;
                }
                else
                {
                    rdbHorizontal.Checked = true;
                }
            }
        }

        public String TabTheme
        {
            get
            {
                return ddlTheme.SelectedValue;
            }
            set
            {
                if(ddlTheme.Items.Count == 0)
                    GetThemes();
                ddlTheme.SelectedValue = value;                
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.parentEditorPart = this.Parent as TabConfigurationEditorPart;

            // Check whether this is the first Page_Load of the control.
            if (this.hiddenFieldDetectRequest.Value == "0")
            {
                this.hiddenFieldDetectRequest.Value = "1";                
            }            

        }

        private void ApplyChanges()
        {
            // Call the ApplyChanges method of the Parent Editor Part class.    
            this.TabsEnabled = chkEnabled.Checked;
            if (rdbVertical.Checked)
            {
                this.TabOrientation = TabOrientationEnum.vertical;
            }
            else
            {
                this.TabOrientation = TabOrientationEnum.horizontal;
            }
            this.TabTheme = ddlTheme.SelectedValue;

            this.parentEditorPart.ApplyChanges();
        }

        private void GetThemes()
        {
            string spSetupPath = SPUtility.GetGenericSetupPath(string.Empty);
            string themesFolderPath = @"TEMPLATE\LAYOUTS\SUIE\css\jquery-ui";
            foreach (string f in Directory.GetDirectories(spSetupPath + themesFolderPath))
            {
                ddlTheme.Items.Add(f.Replace(spSetupPath + themesFolderPath+@"\",""));
            }
            
        }  
    }
}
